/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoFacadeNI;

import ourMessage.Hello;
import ourMessage.Bye;
import ourMessage.ConnexionUDPServer;
import ourMessage.ConnexionUDPClient;
import ourMessage.Text;
import ChateoController.*;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 *
 * @author adrian
 * @author olga
 */
public class FacadeNI {
    private Controller theController;
    private ConnexionUDPServer UDPServer;
    /* OBSELETE
    public FacadeNI(Controller theController, ConnexionUDPServer UDPServer, int port){
        this.theController=theController;
        this.UDPServer=UDPServer;
    }
    */
    public FacadeNI(Controller father, int port) throws SocketException{
        this.theController=father;
        this.UDPServer=new ConnexionUDPServer(port,this);
    }

    public Controller getTheController() {
        return theController;
    }

    public ConnexionUDPServer getUDPServer() {
        return UDPServer;
    }

    public void setTheController(Controller theController) {
        this.theController = theController;
    }

    public void setUDPServer(ConnexionUDPServer UDPServer) {
        this.UDPServer = UDPServer;
    }
    
    public void incommingHello(String remoteLogin , InetAddress remoteAddr) throws UnknownHostException{
        theController.newUser(remoteLogin, remoteAddr);
    }
    public void outgoingHello(InetAddress addr,String myLogin) throws UnknownHostException, SocketException{
        ConnexionUDPClient sender = new ConnexionUDPClient();
        try{
        sender.sendMessage(addr, new Hello(myLogin,false));
        }
        catch (Exception e){
            System.out.println("Tentative d'envoi de message échouée");
        }
        System.out.println("The client" +myLogin+ " has been connected. Message sent \n");
    
    }
    public void incommingBye(String remoteLogin, InetAddress remoteAddr) throws UnknownHostException{
        theController.userLeft(remoteLogin, remoteAddr);
    }
    public void outgoingBye(InetAddress addr,String myLogin) throws UnknownHostException, SocketException{
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(addr, new Bye(myLogin));
    
    }
    public void incommingAck(String remoteLogin , InetAddress remoteAddr) throws UnknownHostException{
        theController.newUserAck(remoteLogin, remoteAddr);
    }
    public void outgoingAck(InetAddress addr,String myLogin) throws UnknownHostException, SocketException{
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(addr, new Hello(myLogin,true));
    }
    public void outgoingText(InetAddress remoteAddr, String myLogin, String text, boolean privato) throws SocketException{
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(remoteAddr, new Text(myLogin,text,privato));
    }
    public void incommingText(InetAddress remoteAddr,String remoteLogin, String text, boolean privato){
        theController.newTextMessage(remoteLogin, remoteAddr,text,privato);
    }
    
    
}
